package com.coship.vas.dapg.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtils {

	/**
	 * 获取两个日期之间的实际天数，支持跨年
	 */
	public static int getDaysBetween(Calendar start, Calendar end) {
		int days = 0;
		if (start.before(end)) {
			days = end.get(Calendar.DAY_OF_YEAR)
					- start.get(Calendar.DAY_OF_YEAR);
			int y2 = end.get(Calendar.YEAR);
			if (start.get(Calendar.YEAR) != y2) {
				start = (Calendar) start.clone();
				do {
					days += start.getActualMaximum(Calendar.DAY_OF_YEAR);
					start.add(Calendar.YEAR, 1);
				} while (start.get(Calendar.YEAR) != y2);
			}
		}
		return days;
	}

	public static void main(String[] args) {
		Calendar start = Calendar.getInstance();
		start.set(2011, 7, 15);
		Calendar end = Calendar.getInstance();
		end.set(2011, 5, 12);

		System.out.println(getDaysBetween(start, end));
	}

	/**
	 * 
	 * Description : 字符串日期转换成Date
	 * 
	 * @param sStr
	 * @return
	 * 
	 */
	public static Date strToDate(String sStr) {
		if (sStr == null)
			return null;
		SimpleDateFormat formatter;
		if (sStr.length() == 19) {
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else if (sStr.length() == 10) {
			formatter = new SimpleDateFormat("yyyy-MM-dd");
		} else if (sStr.length() == 8) {
			formatter = new SimpleDateFormat("yyyyMMdd");
		} else if (sStr.length() == 14) {
			formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		} else {
			formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		}
		ParsePosition pos = new ParsePosition(0);
		// formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		return formatter.parse(sStr, pos);
	}

	/**
	 * 
	 * Description : 获取当前系统时间
	 * 
	 * @param fromFormat
	 *            : yyyy-MM-dd HH:mm:ss
	 * @return
	 * 
	 */
	public static String getNowTime(String fromFormatStr) {
		String now = "";
		if (fromFormatStr == null || "".equals(fromFormatStr)) {
			return "";
		}
		Pattern pattern = Pattern
				.compile("^(y{4})(-{0,1})(MM)(-{0,1})(dd)( {0,1})(H{0,2})(:{0,1})(m{0,2})(:{0,1})(s{0,2})$");
		Matcher match = pattern.matcher(fromFormatStr);
		if (match.find()) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					fromFormatStr);
			Date date = new Date();
			now = simpleDateFormat.format(date);
		}
		return now;
	}

	/**
	 * YYYY-MM-DD HH:MM:SS ,YYYY-MM-DD
	 * 
	 * @param regex
	 * @param value
	 * @return
	 */
	public static boolean checkTimeFormat(String regexRule, String value) {
		return ReglexUtils.regex(regexRule, value);
	}

	/**
	 * 得到指定日期的第一天
	 * 
	 * @return
	 */
	public static Date getMonthFirstDay(Calendar c) {
		c.set(Calendar.DATE, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	/**
	 * 得到指定日期的最后一天
	 * 
	 * @return
	 */
	public static Date getMonthLastDay(Calendar c) {
		c.set(Calendar.DATE, 1);
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DATE, -1);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	/**
	 * 得到指定日期的第一天
	 * 
	 * @return
	 */
	public static Date getMonthFirstDay(Date d) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return getMonthFirstDay(c);
	}

	/**
	 * 得到指定日期的最后一天
	 * 
	 * @return
	 */
	public static Date getMonthLastDay(Date d) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return getMonthLastDay(c);
	}

	/**
	 * 
	 * Description : Calendar转换成时间字符串
	 * 
	 * @param calendar
	 * @return
	 * 
	 */
	public static String getTimeByCalendar(Calendar calendar, String formatStr) {
		if (formatStr == null || "".equals(formatStr)) {
			formatStr = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
		String time = simpleDateFormat.format(calendar.getTime());
		return time;
	}

	public static String DataToStr(Date date, String formatStr) {
		String time = "";
		if (date != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
			time = simpleDateFormat.format(date);
		}
		return time;
	}
}
